/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSGeometricElement.cpp
 *
 *  Created on: 2010/02/16
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSGeometricElement.h"
#include "RSSource.h"

namespace Realsphere {

    RSGeometricElement::RSGeometricElement ( RSGeometricElementType rGeometricElementType ) :
                RSElement ( RSGeometricElement::GeometricElementTypeToTag ( rGeometricElementType ) )
    {
        mGeometricElementType = rGeometricElementType;
    }

    RSGeometricElement::~RSGeometricElement ()
    {
        mSources.clear ();
    }

    RSSource* RSGeometricElement::GetSource ( const QString& rId )
    {
        RSSourcesIterator iterator = RSSourcesIterator ( mSources );

        while ( iterator.hasNext () )
        {
            RSSource* source = iterator.next ();
            QString source_id = source->Id ();

            if ( source_id == rId )
                return source;
        }

        return NULL;
    }

    RSSource* RSGeometricElement::GetSource ( unsigned int rIndex )
    {
        return mSources[rIndex];
    }

    int RSGeometricElement::IndexOf ( RSSource* rSource )
    {
        return mSources.indexOf ( rSource );
    }

    unsigned int RSGeometricElement::SourcesSize ()
    {
        return mSources.size ();
    }

    RSSources RSGeometricElement::Sources () const
    {
        return mSources;
    }

    RSSourcesIterator RSGeometricElement::SourcesIterator () const
    {
        return RSSourcesIterator ( mSources );
    }

    bool RSGeometricElement::IsRootElement () const
    {
        return true;
    }

    RSGeometricElementType RSGeometricElement::GeometricElementType ()
    {
        return mGeometricElementType;
    }

    RSError RSGeometricElement::BeforeAddChildElement ( RSElement* rChild )
    {
        RSError error = RSElement::BeforeAddChildElement ( rChild );

        if ( error == RS_INVALID_ELEMENT )
        {
            if ( rChild->InstanceOf ( RS_SOURCE_CLASS ) )
                error = RS_NO_ERROR;
        }

        return error;
    }

    bool RSGeometricElement::AfterAddChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterAddChildElement ( rChild );

        if ( !handled && rChild->InstanceOf ( RS_SOURCE_CLASS ) )
        {
            RSSource* source = RSSourceCast ( rChild );
            mSources.append ( source );
            handled = true;

            emit SourceAdded ( this, source );
        }

        return handled;
    }

    bool RSGeometricElement::AfterRemoveChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterRemoveChildElement ( rChild );
        int index;

        if ( !handled && rChild->InstanceOf ( RS_SOURCE_CLASS ) )
        {
            RSSource* source = RSSourceCast ( rChild );
            index = mSources.indexOf ( source );
            mSources.removeAt ( index );
            handled = true;

            emit SourceRemoved ( this, source );
        }

        return handled;
    }

    RSError RSGeometricElement::AddSource ( RSSource* rSource )
    {
        RSError error = AddChildElement ( rSource );
        return error;
    }

    RSError RSGeometricElement::RemoveSource ( RSSource* rSource )
    {
        RSError error = RemoveChildElement ( rSource );
        return error;
    }

    RSError RSGeometricElement::RemoveSource ( const QString& rId )
    {
        RSSource* source = GetSource ( rId );
        RSError error = RemoveSource ( source );
        return error;
    }

    RSError RSGeometricElement::RemoveSource ( unsigned int rIndex )
    {
        RSSource* source = GetSource ( rIndex );
        RSError error = RemoveSource ( source );
        return error;
    }

    QString RSGeometricElement::GeometricElementTypeToTag ( RSGeometricElementType rGeometricElementType )
    {
        switch ( rGeometricElementType )
        {
            case RS_CONVEX_MESH:
                return QString ( RS_CONVEX_MESH_TAG );
                break;
            case RS_MESH:
                return QString ( RS_MESH_TAG );
                break;
            case RS_SPLINE:
                return QString ( RS_SPLINE_TAG );
                break;
            case RS_BREP:
                return QString ( RS_BREP_TAG );
                break;
            default:
                return QString ( RS_ELEMENT_TAG );
                break;
        }
    }

    RSGeometricElementType RSGeometricElement::GeometricElementTagToType ( const QString& rTag )
    {
        if ( rTag == RS_CONVEX_MESH_TAG )
        {
            return RS_CONVEX_MESH;
        }
        else if ( rTag == RS_MESH_TAG )
        {
            return RS_MESH;
        }
        else if ( rTag == RS_SPLINE_TAG )
        {
            return RS_SPLINE;
        }
        else if ( rTag == RS_BREP_TAG )
        {
            return RS_BREP;
        }

        return RS_GEOMETRIC_ELEMENT_UNKNOWN;
    }
}
